using UnityEngine;
using UnityEngine.Networking;

public class Airplane : Vehicle
{
	[Header("Airplane")]
	public float FlyDistance = 1000f;

	public float EjectDistance = 500f;

	public float Speed = 20f;

	public bool DropSupply;

	public GameObject DropObject;

	private Vector3 initialPos;

	private Vector3 dropPos;

	private bool dropped;

	private bool ejected;

	private static int kCmdCmdDrop;

	private static int kCmdCmdRemove;

	private static int kRpcRpcRemove;

	public override void Start()
	{
		initialPos = base.transform.position;
		dropped = false;
		ejected = false;
		base.Start();
	}

	public void SetDrop(Vector3 position)
	{
		dropPos = position;
		dropPos.y = 0f;
	}

	[Command(channel = 0)]
	public void CmdDrop()
	{
		if (!dropped)
		{
			UnitZ.gameNetwork.RequestSpawnObject(DropObject, base.transform.position, DropObject.transform.rotation);
			dropped = true;
		}
	}

	[Command(channel = 0)]
	public void CmdRemove()
	{
		CallRpcRemove();
	}

	[ClientRpc(channel = 0)]
	public void RpcRemove()
	{
		UnityEngine.Object.Destroy(base.gameObject);
	}

	public override void Update()
	{
		base.transform.position += base.transform.forward * Speed * Time.deltaTime;
		if (base.isServer)
		{
			if (DropSupply)
			{
				Vector3 position = base.transform.position;
				position.y = 0f;
				if (Vector3.Distance(position, dropPos) <= 20f)
				{
					CallCmdDrop();
				}
			}
			float num = Vector3.Distance(initialPos, base.transform.position);
			if (num >= EjectDistance && !ejected)
			{
				EjectAllSeat();
				ejected = true;
			}
			if (num >= FlyDistance)
			{
				CallCmdRemove();
			}
		}
		base.Update();
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdDrop(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdDrop called on client.");
		}
		else
		{
			((Airplane)obj).CmdDrop();
		}
	}

	protected static void InvokeCmdCmdRemove(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRemove called on client.");
		}
		else
		{
			((Airplane)obj).CmdRemove();
		}
	}

	public void CallCmdDrop()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdDrop called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdDrop();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdDrop);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdDrop");
	}

	public void CallCmdRemove()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRemove called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRemove();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRemove);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdRemove");
	}

	protected static void InvokeRpcRpcRemove(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcRemove called on server.");
		}
		else
		{
			((Airplane)obj).RpcRemove();
		}
	}

	public void CallRpcRemove()
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcRemove called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcRemove);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendRPCInternal(networkWriter, 0, "RpcRemove");
	}

	static Airplane()
	{
		kCmdCmdDrop = 535750849;
		NetworkBehaviour.RegisterCommandDelegate(typeof(Airplane), kCmdCmdDrop, InvokeCmdCmdDrop);
		kCmdCmdRemove = -150764074;
		NetworkBehaviour.RegisterCommandDelegate(typeof(Airplane), kCmdCmdRemove, InvokeCmdCmdRemove);
		kRpcRpcRemove = 631304492;
		NetworkBehaviour.RegisterRpcDelegate(typeof(Airplane), kRpcRpcRemove, InvokeRpcRpcRemove);
		NetworkCRC.RegisterBehaviour("Airplane", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool flag = base.OnSerialize(writer, forceAll);
		bool flag2 = default(bool);
		return flag2 | flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		base.OnDeserialize(reader, initialState);
	}
}
